package icu.groot.alan.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.groot.alan.hotel.entity.dao.AccountDo;
import icu.groot.alan.hotel.entity.dao.OrderDo;
import icu.groot.alan.hotel.entity.dao.RoomDo;
import icu.groot.alan.hotel.enums.OrderStatusEnum;
import icu.groot.alan.hotel.mapper.AccountMapper;
import icu.groot.alan.hotel.mapper.OrderMapper;
import icu.groot.alan.hotel.mapper.RoomMapper;
import icu.groot.alan.hotel.service.OrderService;
import icu.groot.alan.hotel.util.BizException;
import icu.groot.alan.hotel.util.ResultCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderDo> implements OrderService {
    @Resource
    AccountMapper accountMapper;
    @Resource
    RoomMapper roomMapper;
    @Resource
    OrderMapper orderMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void purchase(String orderCode, Integer accCode) {

        QueryWrapper<OrderDo> orderDoQueryWrapper = new QueryWrapper<>();
        orderDoQueryWrapper.eq("order_code", orderCode);
        orderDoQueryWrapper.eq("acc_code", accCode);
        //查询该订单
        OrderDo orderDo = orderMapper.selectOne(orderDoQueryWrapper);
        if (orderDo == null){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("查无此订单"));
        }

        // 查询个人信息
        AccountDo accountDo = accountMapper.selectById(accCode);
        // BigDecimal比较大小，-1小于，0等于，1大于，>-1大于等于，<1小于等于
        if (accountDo.getAccBalance().compareTo(orderDo.getOrderReversePrice()) == -1){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("余额不足"));
        }

        // 查询房间信息
        QueryWrapper<RoomDo> roomDoQueryWrapper = new QueryWrapper<>();
        roomDoQueryWrapper.eq("room_type", orderDo.getRoomType());
        RoomDo roomDo = roomMapper.selectOne(roomDoQueryWrapper);
        if (roomDo.getRoomSpareNum() <= 0){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("抱歉,该类房间已被抢购完"));
        }
        System.out.println(roomDo.toString());

        //修改用户余额
        accountDo.setAccBalance(accountDo.getAccBalance().subtract(orderDo.getOrderReversePrice()));
        if (accountMapper.updateById(accountDo) != 1){
            throw new BizException(ResultCodeEnum.SYSTEM_EXCEPTION);
        }

        //修改订单状态
        orderDo.setOrderStatus(OrderStatusEnum.REVERSE.toString());
        if (orderMapper.updateById(orderDo) != 1){
            throw new BizException(ResultCodeEnum.SYSTEM_EXCEPTION);
        }

    }

    @Override
    public List<OrderDo> listByTime(LocalDateTime checkinStartTime, LocalDateTime checkinEndTime) {
        QueryWrapper<OrderDo> orderDoQueryWrapper = new QueryWrapper<>();
        orderDoQueryWrapper.and(wrapper -> {
            wrapper.le("checkin_start_time", checkinStartTime).gt("checkin_end_time", checkinStartTime);
        });
        orderDoQueryWrapper.or(wrapper -> {
            wrapper.le("checkin_start_time", checkinEndTime).gt("checkin_end_time", checkinEndTime);
        });
        return this.list(orderDoQueryWrapper);
//        return orderMapper.selectByTime(checkinStartTime, checkinEndTime);
    }
}
